package com.library.admin.controller;

import com.library.admin.modules.borrowing.vo.BorrowingVO;
import com.library.admin.modules.borrowing.bo.BorrowingPage;
import com.library.admin.modules.borrowing.bo.BorrowingInsert;
import com.library.admin.modules.borrowing.bo.BorrowingUpdate;
import com.library.admin.modules.borrowing.service.BorrowingService;
import com.library.common.annotation.CurrentUser;
import com.library.common.enums.RoleTypeEnum;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Objects;


/**
 * @Author xyh
 * @Desc 借阅记录表(Borrowing)表控制层
 * @Date 2023-11-09 13:35:13
 */

@RestController
@RequestMapping("borrowing")
public class BorrowingController {

    @Resource
    private BorrowingService borrowingService;


    /**
     * 分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "图书借阅分页查询列表")
    @GetMapping("/list")
    public Result<IPage<BorrowingVO>> queryByPage(@Valid BorrowingPage page, @CurrentUser CurrentLoginUser currentLoginUser) {
        List<Integer> roleIds = currentLoginUser.getRoleIds();
        if (roleIds.contains(RoleTypeEnum.SUPER_ADMIN.getCode()) || roleIds.contains(RoleTypeEnum.LIBRARY_ADMIN.getCode())) {
            page.setUserId(null);
        } else {
            page.setUserId(currentLoginUser.getUserId());
        }
        return Result.success(borrowingService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个借阅记录表数据
     */
    @LogSys(value = "根据图书借阅ID获取数据")
    @PostMapping("/queryById/{id}")
    public Result<BorrowingVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(borrowingService.queryById(id));
    }


    /**
     * 添加借阅记录表
     *
     * @return 新增借阅记录表数据
     */
    @LogSys(value = "新增借阅记录表数据")
    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody BorrowingInsert param, @CurrentUser CurrentLoginUser currentLoginUser) {
        param.setRealName(currentLoginUser.getUsername());
        param.setUserId(currentLoginUser.getUserId());
        param.setJobNumber(currentLoginUser.getJobNumber());
        if (param.getQuantityNum() == null) {
            return Result.error("借阅数量不能为空");
        }
        borrowingService.insert(param);
        return Result.success();
    }


    /**
     * 编辑借阅记录表
     *
     * @return 编辑借阅记录表数据
     */
    @LogSys(value = "编辑借阅记录表数据")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody BorrowingUpdate param) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        borrowingService.update(param);
        return Result.success();
    }

    /**
     * 删除借阅记录表
     *
     * @return 删除借阅记录表数据
     */
    @LogSys(value = "删除借阅记录表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        borrowingService.deleteById(id);
        return Result.success();
    }

    /**
     * 还书
     * @param id
     * @return
     */
    @LogSys(value = "图书归还")
    @PostMapping ("/return/{id}")
    public Result returnBook(@PathVariable("id") Integer id) {
        borrowingService.returnBook(id);
        return Result.success();
    }
}
